# coding: utf8
# models/plugin_plugin.py
## This file was created using static/plugin_plugin/model_template using the plugin_plugin
# copyright Christopher Steel, 2011, Voice of Access
#########################################
# initialization.py
import os
cwd = os.getcwd()
current_app = os.path.join(cwd,'applications',request.application)
#initialization_file_path = os.path.join(current_app,'models','plugin_uc_language','000_uc_initialization.py')
initialization_file_path = os.path.join(current_app,'models','000_uc_initialization.py')

####################################
plugin_uc_language_installed = True
#plugin_debug_enabled = True
#########################################################################
## - Manual Language Change Code -
#########################################################################
#
# languages that don't need translation:
T.current_languages = ['en', 'en-en', 'en-ca']
    
# select user specified _languageuage (via args, session or browser config)
if '_language' in request.vars:
    session._language = _language = request.vars._language
elif session._language:
    _language = session._language
elif T.accepted_language is not None:
    _language = T.accepted_language
else:
    _language = "fr-ca"
T.force(_language)

####################################
# do we hav an initialization file?
#
def create_initialization_file():
    if not os.path.exists(initialization_file_path):
        our_file = open(initialization_file_path,"w")
        our_text = '# file created by plugin_uc_language\n'
        print('created %s' % initialization_file_path)
    else:
        if 'plugin_debug_enabled' in globals():
            if plugin_debug_enabled == True:
                print('found %s' % initialization_file_path)

def append_initialization_file(append):
    our_file = open(initialization_file_path,"w")
    our_file.writelines(append)
    our_file.close()
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print('appended %s to %s' % (append,initialization_file_path))

if not 'initialization_file_created' in globals():
    create_initialization_file()
    append = '# entry created by plugin_uc_language\n'
    append_initialization_file(append)
    append = "initialization_file_created = True\n"
    append_initialization_file(append)
else:
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print("found 'initialization_file_created' in globals")

if not 'group_plugin_uc_language_admin_created' in globals():
    if not db(db.auth_group.role=='plugin_uc_language_admin').select():
        db.auth_group.insert(role='plugin_uc_language_admin',description='user to administer plugin_uc_language')
    else:
        if 'plugin_debug_enabled' in globals():
            if plugin_debug_enabled == True:
                print("found db.auth_group.role=='admin_plugin_plugin' in database")
    append_initialization_file('# entry created by plugin_uc_language\n')
    append_initialization_file('group_plugin_uc_language_admin_created = True\n')
else:
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print('found "group_plugin_uc_language_admin_created" in globals')

###############################################
# ensure we have an config group for our plugin
#
if not 'group_plugin_uc_language_config_created' in globals():
    if not db(db.auth_group.role=='plugin_uc_language_config').select(): 
        db.auth_group.insert(role='plugin_uc_language_config',description='user to configure plugin_uc_language')
        append = '# entry created by plugin_uc_language\n'
        append_initialization_file(append)
        append = 'group_plugin_uc_language_config_created = True\n'
        append_initialization_file(append)
    else:
        if 'plugin_debug_enabled' in globals():
            if plugin_debug_enabled == True:
                print("found db.auth_group.role=='config_plugin_plugin' in database")
else:
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print('found "group_plugin_uc_language_config_created" in globals')

#################################################################
# Ensure we have the global variable 'plugin_uc_language_installed'
#
if not 'plugin_uc_language_installed' in globals():
    our_file = open(initialization_file_path,"a")
    append = '# entry created by plugin_uc_language\n'
    append_initialization_file(append)
    append = 'plugin_uc_language_installed = True\n'
    append_initialization_file(append)
else:
    if 'plugin_debug_enabled' in globals():
        if plugin_debug_enabled == True:
            print('found "models/plugin_uc_language/initialization.py" in globals')
            print('found "plugin_uc_language_installed" in globals')



response.menu_language = []
if T.accepted_language != 'en':
    response.menu_language += [
        ('english', False, URL(r=request,vars=dict(_language='en')),
    )]    
if T.accepted_language != 'fr' and T.accepted_language != 'fr-ca' and T.accepted_language != 'fr-fr':
    response.menu_language += [
        ('français', False, URL(r=request,vars=dict(_language='fr-ca')),
    )]

response.menu += response.menu_language
#
